#!/usr/bin/env python
# Copyright (c) 2007-8 Qtrac Ltd. All rights reserved.
# This program or module is free software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License as published
# by the Free Software Foundation, either version 2 of the License, or
# version 3 of the License, or (at your option) any later version. It is
# provided for educational purposes and is distributed in the hope that
# it will be useful, but WITHOUT ANY WARRANTY; without even the implied
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
# the GNU General Public License for more details.

import os
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtSql import *

MAC = "qt_mac_set_native_menubar" in dir()

ID_CB_NOAWAL, NO_AWAL,  TIPE,  KET,  VISMAS= range(5)


class cbNoAwalDlg(QDialog):

    def __init__(self, parent=None):
        super(cbNoAwalDlg, self).__init__(parent)

        self.model = QSqlTableModel(self)
        self.model.setTable("cb_noawal")
        self.model.setSort(NO_AWAL, Qt.AscendingOrder)
        self.model.setHeaderData(ID_CB_NOAWAL, Qt.Horizontal, QVariant("ID"))
        self.model.setHeaderData(NO_AWAL, Qt.Horizontal, QVariant("No Awal"))
        self.model.setHeaderData(TIPE, Qt.Horizontal, QVariant("Tipe"))
        self.model.setHeaderData(KET, Qt.Horizontal, QVariant("Ket"))
        self.model.setHeaderData(VISMAS, Qt.Horizontal, QVariant("Visa/Master"))
        self.model.select()

        self.view = QTableView()
        self.view.setModel(self.model)
        self.view.setSelectionMode(QTableView.SingleSelection)
        self.view.setSelectionBehavior(QTableView.SelectRows)
        self.view.setColumnHidden(ID_CB_NOAWAL, True)
        self.view.resizeColumnsToContents()

        buttonBox = QDialogButtonBox()
        addButton = buttonBox.addButton("&Add",
                QDialogButtonBox.ActionRole)
        deleteButton = buttonBox.addButton("&Delete",
                QDialogButtonBox.ActionRole)
        sortButton = buttonBox.addButton("&Sort",
                QDialogButtonBox.ActionRole)
        if not MAC:
            addButton.setFocusPolicy(Qt.NoFocus)
            deleteButton.setFocusPolicy(Qt.NoFocus)
            sortButton.setFocusPolicy(Qt.NoFocus)

        menu = QMenu(self)
        sortByNoAction = menu.addAction("Sort by &No AWal")
        sortByTipeAction = menu.addAction("Sort by &Tipe")
        sortByKetAction = menu.addAction("Sort by &Ket")
        sortButton.setMenu(menu)
        closeButton = buttonBox.addButton(QDialogButtonBox.Close)

        layout = QVBoxLayout()
        layout.addWidget(self.view)
        layout.addWidget(buttonBox)
        self.setLayout(layout)

        self.connect(addButton, SIGNAL("clicked()"), self.addRecord)
        self.connect(deleteButton, SIGNAL("clicked()"), self.deleteRecord)
        self.connect(sortByNoAction, SIGNAL("triggered()"), lambda: self.sort(NO_AWAL))
        self.connect(sortByTipeAction, SIGNAL("triggered()"), lambda: self.sort(TIPE))
        self.connect(sortByKetAction, SIGNAL("triggered()"), lambda: self.sort(KET))
        self.connect(closeButton, SIGNAL("clicked()"), self.accept)

        self.setWindowTitle("No Awal Data")


    def addRecord(self):
        row = self.model.rowCount()
        self.model.insertRow(row)
        index = self.model.index(row, NO_AWAL)
        self.view.setCurrentIndex(index)
        self.view.edit(index)


    def deleteRecord(self):
        index = self.view.currentIndex()
        if not index.isValid():
            return
        record = self.model.record(index.row())
        noAwal = record.value(NO_AWAL).toString()
        tipe = record.value(TIPE).toString()
        if QMessageBox.question(self, "Reference Data", QString("Delete %1 from category %2?") .arg(tipe).arg(noAwal), QMessageBox.Yes|QMessageBox.No) == QMessageBox.No:
            return
        self.model.removeRow(index.row())
        self.model.submitAll()


    def sort(self, column):
        self.model.setSort(column, Qt.AscendingOrder)
        self.model.select()


def main():
    app = QApplication(sys.argv)

    db = QSqlDatabase.addDatabase("QMYSQL")
    db.setDatabaseName("mmcdev")
    db.setUserName("root")
    db.setPassword("mtsbede")
    db.setHostName("localhost")
    db.setPort(3306)
    if not db.open():
        QMessageBox.warning(None, "Reference Data", QString("Database Error: %1").arg(db.lastError().text()))
        sys.exit(1)

    form = cbNoAwalDlg()
    form.show()
    sys.exit(app.exec_())


main()

